﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.Text;
using Facade.Excel;

namespace PeisongWeb.admin
{
    public partial class orderReportList :adminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("DI", 1))
                {
                    lbtn_Import.Visible = power == 2 ? true : false;
                    drpBind();
                    rptBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }
        }
        private void drpBind()
        {
            drp_Status.DataSource = logic.sysCode.orderStatus();
            drp_Status.DataBind();
            drp_Status.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
        }

        protected void lbtn_Search_Click(object sender, EventArgs e)
        {
            rptBind();
        }

        public void rptBind()
        {
            StringBuilder where = new StringBuilder();
            where.Append("1=1");
            if (txt_OrderNo.Text.Trim() != "")
                where.AppendFormat(" and [orderNo] ='{0}'", txt_OrderNo.Text.Trim());

            if (ext_StartInputDt.SelectedDate != DateTime.MinValue)
                where.AppendFormat(" and datediff(day,[orderDt],'{0}')<=0", ext_StartInputDt.Value.ToString());
            if (ext_EndInputDt.SelectedDate != DateTime.MinValue)
                where.AppendFormat(" and datediff(day,[orderDt],'{0}')>=0", ext_EndInputDt.Value.ToString());
            if (drp_Status.SelectedItem.Value != "")
                where.AppendFormat(" and [status] in ({0})", drp_Status.SelectedItem.Value);

            DataSet ds = logic.order.select(where.ToString(), "orderId desc", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);

            int recourdCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            AspNetPager1.RecordCount = recourdCount;
            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }

        protected void lbtn_Import_Click(object sender, EventArgs e)
        {
            StringBuilder where = new StringBuilder();
            where.Append("1=1");
            if (txt_OrderNo.Text.Trim() != "")
                where.AppendFormat(" and [orderNo] ='{0}'", txt_OrderNo.Text.Trim());

            if (ext_StartInputDt.SelectedDate != DateTime.MinValue)
                where.AppendFormat(" and datediff(day,[orderDt],'{0}')<=0", ext_StartInputDt.Value.ToString());
            if (ext_EndInputDt.SelectedDate!=DateTime.MinValue)
                where.AppendFormat(" and datediff(day,[orderDt],'{0}')>=0", ext_EndInputDt.Value.ToString());


            if (drp_Status.SelectedItem.Value != "")
                where.AppendFormat(" and [status]='{0}'", drp_Status.SelectedItem.Value);


            DataTable dt = logic.order.listByImportExcel(where.ToString() + " order by orderId desc");
            dt.Columns["orderNo"].ColumnName = "订单号";
            dt.Columns["statusName"].ColumnName = "订单状态";
            dt.Columns["amount"].ColumnName = "订单总价";
            dt.Columns["orderDt"].ColumnName = "订单日期";
            dt.Columns["company"].ColumnName = "采购企业";
            dt.Columns["contact"].ColumnName = "收货人";
            dt.Columns["contactMobile"].ColumnName = "联系人手机";
            dt.Columns["contactPhone"].ColumnName = "联系电话";
            dt.Columns["address"].ColumnName = "收货地址";
            dt.Columns["note"].ColumnName = "备注";
            dt.Columns["serviceNote"].ColumnName = "客服描述";
            dt.Columns["adminName"].ColumnName = "操作员";

            ExportFacade facadeInstance = new ExportFacade();
            facadeInstance.ExportByWeb(dt, "订单数据", "订单数据.xls", null, "订单数据");
        }
    }
}
